package org.glow.changgou.user.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.user.dao.AreasMapper;
import org.glow.changgou.user.pojo.Address;
import org.glow.changgou.user.pojo.Areas;
import org.glow.changgou.user.service.AreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:admin
 * @Description:Areas业务层接口实现类
 * @Date 2022/10/02 20:49:12
 *****/
@Service
public class AreasServiceImpl  extends ServiceImpl<AreasMapper, Areas> implements AreasService {

    @Resource
    private AreasMapper areasMapper;


    /**
     * Areas条件+分页查询
     * @param areas 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Areas> findPage(Areas areas, int page, int size){
        //分页
        Page<Areas> pageInfo = new Page<>(page,size);
        //搜索条件构建
      LambdaQueryWrapper<Areas> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper = getCondition(queryWrapper,areas);
        //执行搜索
        return areasMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * Areas分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Areas> findPage(int page, int size){
        Page<Areas> pageInfo = new Page<>(page,size);
        //执行搜索
        return areasMapper.selectPage(pageInfo,null);
    }

    /**
     * Areas条件查询
     * @param areas
     * @return
     */
    @Override
    public List<Areas> findList(Areas areas){
        LambdaQueryWrapper<Areas> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,areas);
        return areasMapper.selectList(queryWrapper);
    }


    /**
     * Areas构建查询对象
     * @param areas
     * @return
     */
    public LambdaQueryWrapper<Areas> getCondition(LambdaQueryWrapper<Areas> queryWrapper, Areas areas){


        if(areas!=null){
            // 区域ID
            if(!StringUtils.isEmpty(areas.getAreaid())){
//                    criteria.andEqualTo("areaid",areas.getAreaid());
                    queryWrapper.eq(Areas::getAreaid,areas.getAreaid());
            }
            // 区域名称
            if(!StringUtils.isEmpty(areas.getArea())){
//                    criteria.andEqualTo("area",areas.getArea());
                    queryWrapper.eq(Areas::getArea,areas.getArea());
            }
            // 城市ID
            if(!StringUtils.isEmpty(areas.getCityid())){
//                    criteria.andEqualTo("cityid",areas.getCityid());
                    queryWrapper.eq(Areas::getCityid,areas.getCityid());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        areasMapper.deleteById(id);
    }

    /**
     * 修改Areas
     * @param areas
     */
    @Override
    public void update(Areas areas){
        areasMapper.updateById(areas);
    }

    /**
     * 增加Areas
     * @param areas
     */
    @Override
    public void add(Areas areas){
        areasMapper.insert(areas);
    }

    /**
     * 根据ID查询Areas
     * @param id
     * @return
     */
    @Override
    public Areas findById(String id){
        return  areasMapper.selectById(id);
    }

    /**
     * 查询Areas全部数据
     * @return
     */
    @Override
    public List<Areas> findAll() {
        return areasMapper.selectList(null);
    }
}
